﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Thingpower.SS.OSS.Core;
using Thingpower.SS.OSS.Models;

namespace Thingpower.SS.OSS.BackgroundPages
{
    public partial class PageSettings : System.Web.UI.Page
    {
        public PluginConfigInfo _configInfo;

        protected void Page_Load(object sender, EventArgs e)
        {
            _configInfo = Utils.GetConfig();

            if (IsPostBack) return;

            this.txtKeyAccess.Text = _configInfo.KeyAccess;
            this.txtKeySecret.Text = _configInfo.KeySecret;
            this.txtDomain.Text = _configInfo.Domain;
            this.txtBucket.Text = _configInfo.Bucket;
        }

        public void Submit_OnClick(object sender, EventArgs e)
        {
            if (!Page.IsPostBack) return;

            string _keyAccess = this.txtKeyAccess.Text.Trim();
            string _keySecret = this.txtKeySecret.Text.Trim();
            string _domain = this.txtDomain.Text.Trim();
            string _bucket = this.txtBucket.Text.Trim();

            _configInfo.KeyAccess = _keyAccess;
            _configInfo.KeySecret = _keySecret;
            _configInfo.Domain = _domain;
            _configInfo.Bucket = _bucket;

            if (string.IsNullOrEmpty(_configInfo.KeyAccess) || string.IsNullOrEmpty(_configInfo.KeySecret) || string.IsNullOrEmpty(_configInfo.Domain) || string.IsNullOrEmpty(_configInfo.Bucket))
            {
                this.ltlMessage.Text = Utils.GetMessageHtml("设置项不可以为空！", false);
                return;
            }
            OSSPlugin.Instance.ConfigApi.SetConfig(0, _configInfo);

            this.ltlMessage.Text = Utils.GetMessageHtml("插件设置修改成功！", true);
        }
    }
}